package leeon.bbsbrower.views;

import leeon.bbsbrower.actions.BBSLogAction;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class FriendView extends ViewPart {
	public static final String ID = "bbsbrower.view.friend";

	private List list = null;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		
		new Label(parent, SWT.NONE);
		
		//主菜单
		list = new List(parent, SWT.NONE);
		list.setFont(ApplicationWorkbenchWindowAdvisor.defaultFont);
		
		//写死
		list.add("环顾四方");
		list.add("查询网友");
		list.add("在线好友");
		list.add("发送信息");
		list.add("所有信息");
		
		//登陆进来时选中0
		list.select(0);
		
		//上下左右的事件
		list.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//左
				if (e.keyCode == SWT.ARROW_LEFT) {
					actionToMain();
					e.doit = false;
				//右
				} else if (e.keyCode == SWT.ARROW_RIGHT) {										
					//环顾四方
					if (list.getSelectionIndex() == 0 || e.keyCode == 13) {
						actionAllUsers();
					//查询网友	
					} else if (list.getSelectionIndex() == 1) {
						actionFindUser();						
					//在线好友
					} else if (list.getSelectionIndex() == 2) {
						actionOnlineFriend();						
					//发送信息
					} else if (list.getSelectionIndex() == 3) {
						actionSendMsg();
					//所有信息
					} else if (list.getSelectionIndex() == 4) {
						actionAllMsg();
					}					
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});
		
		new Label(parent, SWT.NONE);
	}
	
	
	/**
	 * 回到主选单
	 */
	private void actionToMain() {		
		//跳转到主选单
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			MainView main = (MainView)page.showView(MainView.ID);
			main.refreshData(2);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 所有用户
	 */
	private void actionAllUsers() {
		//跳转到所有用户列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			UserListView user = (UserListView)page.showView(UserListView.ID);
			user.refreshData(1, 0);
		} catch (PartInitException e) {
			e.printStackTrace();
		}		
	}
	
	/**
	 * 查找用户
	 */
	private void actionFindUser() {
		if ("guest".equals(BBSLogAction.id)) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "上鹊桥", "请先登陆");;
			return;
		}
		
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			page.showView(FindUserView.ID);			
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 在线好友
	 */
	private void actionOnlineFriend() {
		if ("guest".equals(BBSLogAction.id)) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "上鹊桥", "请先登陆");;
			return;
		}
		
		//跳转到在线好友列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			UserListView user = (UserListView)page.showView(UserListView.ID);
			user.refreshData(0, 0);
		} catch (PartInitException e) {
			e.printStackTrace();
		}		
	}
	
	/**
	 * 发送消息
	 */
	private void actionSendMsg() {
		if ("guest".equals(BBSLogAction.id)) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "上鹊桥", "请先登陆");;
			return;
		}
		
		//跳转到发送消息界面
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			page.showView(SendMsgView.ID);			
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 所有消息
	 */
	private void actionAllMsg() {
		if ("guest".equals(BBSLogAction.id)) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "上鹊桥", "请先登陆");;
			return;
		}
		
		//跳转到在所有消息列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			AllMsgView am = (AllMsgView)page.showView(AllMsgView.ID);
			am.refreshData();
		} catch (PartInitException e) {
			e.printStackTrace();
		}		

	}
	
	/**
	 * 刷新数据
	 *
	 */
	public void refreshData(int index) {
		ApplicationActionBarAdvisor.setStatusLine();
		list.select(index);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		list.setFocus();		
	}
}